<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>webgame</title>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="./js/Socket.js"></script>
<script type="text/javascript" src="./js/Astar.js"></script>
<script type="text/javascript" src="./js/Game.js"></script>
<script type="text/javascript" src="./js/Chat.js"></script>
<script type="text/javascript" src="./js/Mask.js"></script>
<script type="text/javascript" src="./js/Event.js"></script>
<script type="text/javascript" src="./js/mapLib.js"></script>
<link type="text/css" rel="stylesheet" href="./css/game.css" media="all" />

<script type="text/javascript">
	//临时函数，随机用户名
	function randomChar(l) {
		var x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
		var tmp = "";
		for(var i = 0; i < l; i++) {
			tmp += x.charAt(Math.ceil(Math.random()*100000000) % x.length);
		}
		return tmp;
	}
	
	//临时函数，为了登陆
	function getKey(nickname) {
		var now_time = new Date();
		var year = now_time.getFullYear();
		var month = now_time.getMonth() + 1;
		var date = now_time.getDate();
		
		month = month < 10 ? "0" + month : month;
		date = date < 10 ? "0" + date : date;

		var now_data = year + '-' + month + '-' + date;
		var data = nickname + 'aaa' + now_data;
		var loginKey;
		ajax.post("getKey.php", "data=" + data, function(data){
			var json = parseJson(data);
			gameSocket.loginKey = json.key;
			statusbar.load('正在连接socket...');
			gameSocket.connect();
		});
		
	}

	var statusbar = {
		load:function(msg){
			var statusobj = document.getElementById("status");
			statusobj.innerHTML = '<img src="images/loading.gif" align="absmiddle"> '+msg;
		},
		finish:function(msg){
			var statusobj = document.getElementById("status");
			statusobj.innerHTML = msg;
			window.setTimeout(function(){statusobj.innerHTML=''}, 1000);
		}
	}
																																		function loadJS(sid, fileUrl) {
		ajax.get(fileUrl, function(data){

//								if (!!dom.getById(sid)) {
//									dom.getById(sid).text = data;
//								}
//								else {
//									var oHead = document.getElementsByTagName('HEAD').item(0);
//									var oScript = document.createElement( "script" );
//
//									oScript.language = "javascript";
//									oScript.type = "text/javascript";
//									oScript.text = data;
//									oScript.id = sid;
//
//									oHead.appendChild(oScript);
//								}
								//IE直接改变script好像不会改变变量值。。。所以我直接运行了
								eval(data);
								statusbar.load('正在生成地图...');

								mygame.loadMap(32, 32, mapsLibs, mapsBg, mapsItem, 10, 100);
								window.astar = new Astar(mygame.g_map.g_flag);
								
								//statusbar.load('地图生成完成，载入事件监听...');

								statusbar.finish('地图生成完成');
							}, false);

	}																																																		   
    addDOMLoadEvent(function() {
        window.mygame = new Game(dom.getById("game1"), 800, 600);
		statusbar.load('正在初始化物件...');
		window.gameChat = new Chat();
		window.gameSocket = new Socket();
  		window.popups = new Popups("popupContainer");
		statusbar.load('正在创造角色...');
		popups.createPopup('Prompt', '游戏用户名', function(username) {
													gameSocket.nickname = username;
													getKey(gameSocket.nickname);
													mygame.curUser = gameSocket.nickname;
												}).show();
		
		gameSocket.sendHandle = function(data) {
			if (data.split('|')[0] == 'W')
				ajax.post("md5.php", "data=" + data + "&private_key=" + gameSocket.privateKey, function(data){
					var json = parseJson(data);
					gameSocket.send(json.data, json.key);
				}); 
			else
				gameSocket.send(data, '0');
		};

    });
</script>
</head>
<body>
<div id="debug"></div>
<div id="popupContainer"></div>
<div id="socket_container">
	<object height="0" width="0" align="middle" id="socket" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">
	<param name="allowScriptAccess" value="sameDomain"/>
	<param name="allowFullScreen" value="false"/>
	<param name="movie" value="./socket.swf"/>
	<param name="quality" value="high"/>
	<param name="allowNetworking" value="all"/>
	<embed height="0" width="0" align="middle" pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" allownetworking="all" allowfullscreen="false" allowscriptaccess="sameDomain" name="socket" quality="high" src="./socket.swf"/>
	</object>
</div>
<div id="game1"></div>
<div id="status" style="position:absolute;left:300px;top:200px;z-index:100;"></div>
<div id="minimap">
</div>
<input type="text" name="chatContent" id="chatContent"/>
</body>
</html>